<template>
  <div class="" style="height: 100%;" v-loading="loadingAll">
    <!-- 搜索框 -->
    <!-- <el-form :inline="true">

      <el-form-item label="状态:">
        <el-select v-model="search.state" placeholder="选择状态" clearable>
          <el-option value="-1" label="待支付"></el-option>
          <el-option value="1" label="待发货"></el-option>
          <el-option value="2" label="待收货"></el-option>
          <el-option value="3" label="待评论"></el-option>
          <el-option value="4" label="评论完成"></el-option>
           <el-option value="5" label="完结"></el-option> -->
        <!-- </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" style="background: #FF2832;" @click="handleSearch">查询
        </el-button>
      </el-form-item>
    </el-form> -->

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">

      <el-table-column label="创建时间" prop="orderTime" align="center" width="200">
      </el-table-column>
    <el-table-column label="商品信息"  align="center" width="200">
        <template slot-scope="scope">
          <span ><img :src='scope.row.coverImg'  style="width:50px;height:50px" >{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="总价格" width="100" align="center">
        <template slot-scope="scope">
          <span style="color: red;font-weight: 600;">{{scope.row.sum}}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.state == 1">待发货</el-button>
          <el-button size="mini" type="primary" v-if="scope.row.state == 2">待收货</el-button>
          <el-button size="mini" type="success" plain v-if="scope.row.state == 3">待评论</el-button>
          <el-button size="mini" type="success" v-if="scope.row.state == 4">评论完成</el-button>
          <!-- <el-button size="mini" type="danger" v-if="scope.row.status == 5">完结</el-button> -->
        </template>
      </el-table-column>

      <el-table-column prop="addressDetail" label="收货地址">
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.state == 1"
            icon="el-icon-office-building" >
            待发货
          </el-button>
          <el-button size="mini" type="text" v-if="scope.row.state == 2" icon="el-icon-s-promotion"
            @click="orderReceive(scope.row.id)">
            确认收货
          </el-button>
          <el-button size="mini" type="text" v-if="scope.row.state == 3" icon="el-icon-s-promotion"
            @click="orderComment(scope.row.id)">
            待评价
          </el-button>
        <el-button size="mini" type="text" v-if="scope.row.state == 4" icon="el-icon-s-promotion"
           >
            已完成
          </el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-document-copy" @click="handleDetail(scope.row)">订单详情
          </el-button> -->
        </template>
      </el-table-column>

    </el-table>

    <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next" :total="total"
      :page-size="rows"  :current-page="page" style="margin: 15px;">
    </el-pagination>

    <el-dialog title="图书详情" :visible.sync="detailDialog" width="60%" :modal="false">

      <div v-for="item in detail" :key="item.id" style="margin: 15px;border:2px solid #f6f6f8;padding: 10px;">

        <el-row :gutter="20">
          <el-col :span="4">
            <div>
              <img :src="item.coverImg" style="width: 100px;display: block;height: 100px;" />
            </div>
          </el-col>
          <el-col :span="6">
            <div style="line-height: 11px;margin:5px ;">
              <span style="font-size: 11px;">{{item.title}}</span>
              <br /><br />
            </div>
          </el-col>
          <el-col :span="4">
            <span style="font-size: 13px;font-weight: 600;line-height: 100px;">单价：￥{{item.price}}</span>
          </el-col>
          <el-col :span="3">
            <span style="font-size: 13px;font-weight: 600;line-height: 100px;">数量：{{item.num}}</span>
          </el-col>
          <el-col :span="4">
            <span style="font-size: 13px;font-weight: 600;line-height: 100px;">金额：<span
                style="color: red;font-size: 15px;">￥{{item.sum}}</span></span>
          </el-col>
          <el-col :span="3" style="margin-top: 35px;" v-if="isComment && item.isComment != 1">
            <el-button type="primary" icon="el-icon-edit" plain size="small" @click="handleComment(item)">评论
            </el-button>
          </el-col>
        </el-row>
      </div>
    </el-dialog>
    <el-dialog width="40%" title="评论" :visible.sync="commentDialog" append-to-body>

        <el-form label-width="80px">
          <el-rate v-model="value" show-text></el-rate>
          <el-form-item label="内容:">
            <el-input v-model="comment" placeholder="请输入评论内容"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="commentDialog = false">取 消</el-button>
          <el-button type="primary" @click="pushComment">提 交</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
/**
   * author        cyi
   * description
   */
import { getOrderById, updateState } from '@/api/order'
export default {
  name: 'orders',
  data () {
    return {
      loading: false,
      loadingAll: false,
      value: null,
      tableData: [],
      page: 1,
      rows: 5,
      total: 0,
      search: {},
      detail: [],
      detailDialog: false,
      commentDialog: false,
      isComment: false,
      comment: '',
      row: null,
      id: 0
    }
  },
  methods: {
    getAllorder () {
      this.loading = true
      const id = JSON.parse(localStorage.getItem('userMsg')).id
      getOrderById(id).then(res => {
        if (res.data.meta.status === 201) {
          this.$message({
            message: res.data.meta.msg,
            type: 'warning'
          })
        } else if (res.data.meta.status === 200) {
          console.log(res.data.data)
          this.tableData = res.data.data
          this.tableData.forEach((item, index) => {
            item.coverImg = 'http://localhost:4096/img/' + item.coverImg
          })
          this.$message({
            message: '查询成功',
            type: 'success'
          })
        }
      })
    },
    handleSearch () {
      this.page = 1
      this.getTable()
    },
    getTable () {
      this.loading = true
      const param = {
        ...this.search
      }
      param.page = this.page
      param.rows = this.rows
      this.$axios.getOrderList(param).then(res => {
        this.tableData = res.data.data
        this.tableData.forEach(i => { i.price = this.$common.returnFloat(i.price) })
        this.total = res.data.total
        this.loading = false
      })
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTable()
    },
    orderReceive (zid) {
      this.$confirm('确认要收货该订单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          id: zid,
          state: 3
        }
        // 传入对象
        updateState(data).then(res => {
          if (res.data.meta.status === 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.getAllorder()
          }
        })
        // this.loadingAll = true
        // this.$axios.receiveCancel(id).then(res => {
        //   this.getTable()
        //   this.$notify({
        //     title: '成功',
        //     message: '订单收获成功！',
        //     type: 'success'
        //   })
        //   this.loadingAll = false
        // }).catch(error => {
        //   this.loadingAll = false
        //   console.log(error)
        // })
      })
    },
    // 打开评论弹窗
    orderComment (id) {
      this.commentDialog = true
      console.log(id)
      this.id = id
    },

    handleDetail (row) {
      this.isComment = row.status === 4
      this.$axios.getOrderDetail(row.id).then(res => {
        res.data.forEach(item => { item.totalPrice = item.price * item.num })
        this.detail = res.data
        this.detailDialog = true
      })
    },
    handleComment (row) {
      this.comment = {}
      this.comment.bookId = row.id
      this.comment.ordersDetailId = row.ordersDetailId
      this.row = row
      this.commentDialog = true
    },
    pushComment () {
      const data = {
        id: this.id,
        comment: this.comment,
        state: 4
      }
      // 传入对象
      updateState(data).then(res => {
        if (res.data.meta.status === 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getAllorder()
          this.commentDialog = false
        }
      })
    }
  },
  created () {
    this.getAllorder()
  }
}
</script>

<style scoped>

</style>
